{% extends 'base.html' %}
{% load static %}

{% block title %}编辑设备 - 维修管理系统{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1>编辑设备</h1>
        <div class="btn-group">
            <a href="{% url 'assets:asset_admin_detail' asset.pk %}" class="btn btn-outline-secondary">
                <i class="bi bi-arrow-left"></i> 返回详情
            </a>
            <a href="{% url 'assets:asset_manage' %}" class="btn btn-outline-secondary">
                <i class="bi bi-list"></i> 设备列表
            </a>
        </div>
    </div>

    {% if messages %}
    <div class="alert alert-info">
        {% for message in messages %}
        <div>{{ message }}</div>
        {% endfor %}
    </div>
    {% endif %}

    <div class="card">
        <div class="card-header">
            <h5 class="card-title mb-0">设备信息</h5>
        </div>
        <div class="card-body">
            <form method="post">
                {% csrf_token %}
                
                <div class="row">
                    <!-- 基本信息 -->
                    <div class="col-md-6">
                        <h6 class="mb-3">基本信息</h6>
                        
                        <div class="mb-3">
                            <label class="form-label">设备名称 *</label>
                            <input type="text" name="name" class="form-control" value="{{ asset.name }}" required>
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">设备编号</label>
                            <input type="text" name="code" class="form-control" value="{{ asset.code|default:'' }}">
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">设备型号</label>
                            <input type="text" name="model" class="form-control" value="{{ asset.model|default:'' }}">
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">序列号</label>
                            <input type="text" name="serial_number" class="form-control" value="{{ asset.serial_number|default:'' }}">
                        </div>
                    </div>

                    <!-- 分类和状态 -->
                    <div class="col-md-6">
                        <h6 class="mb-3">分类和状态</h6>
                        
                        <div class="mb-3">
                            <label class="form-label">设备分类 *</label>
                            <select name="asset_type" class="form-select" required>
                                {% for value, label in category_choices %}
                                <option value="{{ value }}" {% if asset.asset_type == value %}selected{% endif %}>{{ label }}</option>
                                {% endfor %}
                            </select>
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">设备状态 *</label>
                            <select name="status" class="form-select" required>
                                {% for value, label in status_choices %}
                                <option value="{{ value }}" {% if asset.status == value %}selected{% endif %}>{{ label }}</option>
                                {% endfor %}
                            </select>
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">分配用户</label>
                            <select name="assigned_to" class="form-select">
                                <option value="">-- 不分配 --</option>
                                {% for user in users %}
                                <option value="{{ user.username }}" {% if asset.assigned_to == user %}selected{% endif %}>
                                    {{ user.username }} ({{ user.get_full_name|default:user.first_name|default:user.last_name|default:"" }})
                                </option>
                                {% endfor %}
                            </select>
                            <div class="form-text">只能分配给客户角色的用户</div>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <!-- 采购信息 -->
                    <div class="col-md-6">
                        <h6 class="mb-3">采购信息</h6>
                        
                        <div class="mb-3">
                            <label class="form-label">购买日期</label>
                            <input type="date" name="purchase_date" class="form-control" 
                                   value="{% if asset.purchase_date %}{{ asset.purchase_date|date:'Y-m-d' }}{% endif %}">
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">购买价格</label>
                            <input type="number" step="0.01" name="purchase_price" class="form-control" 
                                   value="{{ asset.purchase_price|default:'' }}">
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">保修到期</label>
                            <input type="date" name="warranty_expiry" class="form-control" 
                                   value="{% if asset.warranty_expiry %}{{ asset.warranty_expiry|date:'Y-m-d' }}{% endif %}">
                        </div>
                    </div>

                    <!-- 位置信息 -->
                    <div class="col-md-6">
                        <h6 class="mb-3">位置信息</h6>
                        
                        <div class="mb-3">
                            <label class="form-label">所属部门</label>
                            <input type="text" name="department" class="form-control" value="{{ asset.department|default:'' }}">
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">位置</label>
                            <input type="text" name="location" class="form-control" value="{{ asset.location|default:'' }}">
                        </div>
                        
                        <div class="mb-3">
                            <label class="form-label">备注</label>
                            <textarea name="notes" class="form-control" rows="3">{{ asset.notes|default:'' }}</textarea>
                        </div>
                    </div>
                </div>

                <div class="row">
                    <div class="col-12">
                        <div class="d-flex justify-content-end gap-2">
                            <a href="{% url 'assets:asset_admin_detail' asset.pk %}" class="btn btn-secondary">
                                取消
                            </a>
                            <button type="submit" class="btn btn-primary">
                                <i class="bi bi-check-circle"></i> 保存更改
                            </button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<style>
.card {
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    border: none;
}
.card-header {
    background-color: #f8f9fa;
    border-bottom: 1px solid #e9ecef;
}
h6 {
    color: #6c757d;
    border-bottom: 2px solid #007bff;
    padding-bottom: 0.5rem;
}
</style>
{% endblock %}
